import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

public class Main {
    public static void main(String[] args) {
        //创建druid连接池对象  dateSource 数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/jdbc");
        dataSource.setUsername("root");
        dataSource.setPassword("080472");
        dataSource.setInitialSize(5); // 初始连接池大小
        dataSource.setMinIdle(5); // 最小空闲连接数
        dataSource.setMaxActive(20); // 最大活动连接数
        dataSource.setValidationQuery("select 1"); // 验证连接是否有效的查询语句
        dataSource.setTestOnBorrow(true); // 验证连接是否有效
        dataSource.setTestOnReturn(true);
        dataSource.setTestWhileIdle(true);
        dataSource.setTimeBetweenEvictionRunsMillis(3000);
        dataSource.setMinEvictableIdleTimeMillis(60000);
        dataSource.setMaxWait(5000);
        dataSource.setPoolPreparedStatements(true);


        try {
            DruidPooledConnection connection = dataSource.getConnection();
            if (connection != null) {
                System.out.println("连接成功");
            } else {
                System.out.println("连接失败");
            }

        } catch (Exception e) {
            throw new RuntimeException();
        }


    }
}